import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登陆页面组件
import Login from '@/views/login/index.vue'
// 导入注册页面组件
import Reg from '@/views/Reg/index.vue'
// 导入主页
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home'
import ArtCategory from '@/views/ArtCategory'
import ArtList from '@/views/ArtList'
import UserInfo from '@/views/UserInfo'
import ChangeAvatar from '@/views/ChangeAvatar'
import ResetPwd from '@/views/ResetPwd'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: '/login',
    component: Login
  },
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', component: Home },
      { path: '/artcategory', component: ArtCategory },
      { path: '/artlist', component: ArtList },
      { path: '/userinfo', component: UserInfo },
      { path: '/changeavatar', component: ChangeAvatar },
      { path: '/resetpwd', component: ResetPwd }
    ]
  }
  ]
})
// 路由势力上放置一个导航守卫 控制可不可以访问某些页面
const whiteList = ['/login', '/reg']
router.beforeEach((to, from, next) => {
  // console.log('to', to)
  // console.log('from', from)
  // if (to.path === '/login' || to.path === '/reg') {
  if (whiteList.includes(to.path)) { // includes 可以判断是否含有该字段
    // if (whiteList.indexOf(to.path) !== -1) {

    next()
  } else {
    if (localStorage.getItem('tokenKey')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
